package com.zrwl.poscloud.core.restapi.admin.baseInfo;

import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.base.entity.Dept;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.DeptUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.mapper.DeptMapper;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.DeptService;
import com.zrwl.poscloud.xo.vo.DeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 商户机构 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-02-02
 */
@RestController
@Api(value = "商户机构相关接口", tags = {"商户机构相关接口"})
@RequestMapping("/dept")
@Slf4j
public class DeptRestApi {
    @Autowired
    private DeptService deptService;

    @Autowired
    private AdminService adminService;
    @Resource
    private DeptMapper deptMapper;

    @AuthorityVerify
    @ApiOperation(value = "获取商户机构列表", notes = "获取商户机构列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody DeptVO deptVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取商户机构列表: {}", deptVO);
        return ResultUtil.successWithData(deptService.getPageList(deptVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取商户机构列表", notes = "获取商户机构列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody DeptVO deptVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取商户机构列表: {}", deptVO);
        return ResultUtil.successWithData(deptService.getAllList(deptVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增商户机构")
    @ApiOperation(value = "新增商户机构", notes = "新增商户机构", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody DeptVO deptVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增商户机构: {}", deptVO);
        //根据superCode获取companyUid
            return deptService.add(deptVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改商户机构")
    @ApiOperation(value = "修改商户机构", notes = "修改商户机构", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody DeptVO deptVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改商户机构: {}", deptVO);
        return deptService.edit(deptVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除商户机构")
    @ApiOperation(value = "删除商户机构", notes = "删除商户机构", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody DeptVO deptVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除商户机构: {}", deptVO);
        Dept entity = deptService.getById(deptVO.getUid());
        if (deptMapper.getSubDeptByDeptCode(DeptUtil.getLatestCompnay(entity.getCompanyCode()),entity.getUid()) > 0) {
            throw new RuntimeException("先删除该部门的子部门信息");
        }
        return deptService.delete(deptVO);
    }
}
